﻿// JScript 文件

var imaxStars=50;                     //最多星星数
var imax_Y=413;                       //最大Y坐标值
var imax_X=600;                       //最大X坐标值
var maxWait=1;                        //最大等待时间1秒
var startpoint={x:10,y:400};          //星星飞行起点

var istarFrame_x=new Array(50);       //星座框架数组横坐标
var istarFrame_y=new Array(50);       //星座框架数组列坐标

var iusedStars=0;                     //框架中已使用的星星数
var iusedFrameStars=0;                //星座中已使用完的星星数
var icurrStarId=-1;                   //当前选中的浏览者星星
var iwaitTime=0;                      //以等待时间（秒）
var iInterval;                        //系统间隔调用

var img1;
var img2;
var userImg;
var deleteBlessStar=new Array();

function alterFrame()
{
    invokeHandle();
}

/*激活事件控制函数*/
function invokeHandle()
{
     document.getElementById("mainPic").onkeydown=keyDownHandle;
     for(var i=0;i<iusedStars;i++)
     {
        var obj=document.getElementById("star"+(i+1));
        
        obj.onkeydown=Function("deleteStar("+(i+1)+")");    //删除事件
        
        obj.onmouseover=null;
        obj.onmouseout=null;
        obj.style.visibility="visible";
        Draggable(obj);
        
        obj=document.getElementById("img"+(i+1));
        if(obj.src!=img2.src)
            obj.src=img1.src;
     }
     document.getElementById("locateUserBless").innerHTML="";
}

/*取消事件控制函数*/
function uninvokeHandle()
{
     document.getElementById("mainPic").onkeydown=null;
     for(var i=0;i<iusedStars;i++)
     {
        var obj=document.getElementById("star"+i+1);
        obj.style.visibility="hidden";
        obj.onmousedown=null;
        obj.onkeydown=null;
     }   
}

/*
 ctrl+鼠标左键点击 事件处理函数(主图片上的)
*/
function addStar()
{   
    if(iusedStars>=imaxStars)
    {
        alert("星星数目已经达到50个，不可以在添加了");
        return;
    }
    var x=event.x;
    var y=event.y;
    if(y>=imax_Y)
        y=413-10;
    if(x>=imax_X)
        x=imax_X-10;
    istarFrame_x[iusedStars]=x;
    istarFrame_y[iusedStars]=y;
    iusedStars++;
    var str="";
    str+="<div id=star"+iusedStars+" style='position:absolute; Top:"+y+"px;Left:"+x+"px;"
           +"width:6px;height:3px;background-color:#FFEFFE;'"
           +"<input id=info"+(i+1)+" type=hidden></input>"
           +"<img id=img"+(i+1)+" src="+img1.src+"/></div>";
    document.getElementById("stars").innerHTML+=str;
    
    var obj=document.getElementById("star"+iusedStars);
    obj.onkeydown=Function("deleteStar("+(iusedStars)+")");    //删除事件   
    obj.style.visibility="visible";
    Draggable(obj);
    document.getElementById("mainPic").onmousedown=null;          
}

    function keyDownHandle()
    {
        if(event.keyCode==16)
        {
            document.getElementById("mainPic").onmousedown=addStar;
        }
    }

    /* 移动星星函数（星星上的）*/

    function getStyle(oElm, strCssRule)
    {
        var strValue = "";
        if(document.defaultView && document.defaultView.getComputedStyle){
            var css = document.defaultView.getComputedStyle(oElm, null);
            strValue = css ? css.getPropertyValue(strCssRule) : null;
        }
        else if(oElm.currentStyle){
            strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
                return p1.toUpperCase();
            });
            strValue = oElm.currentStyle[strCssRule];
        }
        return strValue;
    }
    
    function Draggable(el)
    {
        var xDelta = 0, yDelta = 0;
        var xStart = 0, yStart = 0;
        // remove the events
        function enddrag()
        {
            document.onmouseup = null;
            document.onmousemove = null;
        }
        // fire each time it's dragged
        function drag(e)
        {
           if(event.x>=imax_X || event.x<=2)
                return;
           if(event.y>=imax_Y || event.y<=2)
                return;
            e = e || window.event;
            xDelta = xStart - parseInt(e.clientX);
            yDelta = yStart - parseInt(e.clientY);
            xStart = parseInt(e.clientX);
            yStart = parseInt(e.clientY);
            el.style.top = (parseInt(el.style.top) - yDelta) + 'px';
            el.style.left = (parseInt(el.style.left) - xDelta) + 'px';
        }
        // initiate the drag
        function md(e)
        {
            e = e || window.event;
            xStart = parseInt(e.clientX);
            yStart = parseInt(e.clientY);
            el.style.top = parseInt(getStyle(el,'top')) + 'px';
            el.style.left = parseInt(getStyle(el,'left')) + 'px';
            document.onmouseup = enddrag;
            document.onmousemove = drag;
            return false;
        }
        // tie it into the element
        el.onmousedown = md;
    }

    /*删除星星函数(星星上的)*/
    function deleteStar(id)
    {
        if(event.keyCode==46 && iusedStars>0 && id>0)
        {
           if(document.getElementById("img"+id).src==img2.src)
           {
                if(!confirm("此星星已被许愿 ，确定要删除？"))
                    return;
                var s=document.getElementById("info"+id).value;
                deleteBlessStar.push(s);
           }
           for(var i=id;i<iusedStars;i++)
           {   
              istarFrame_x[i-1]=istarFrame_x[i];
              istarFrame_y[i-1]=istarFrame_y[i];
           }
           iusedStars--;
           showStars();
           showStars_User();
           invokeHandle();
        }
        alert(deleteBlessStar[0]);
    }

    /* 显示所有的星座*/
    function showStars()
    {
         document.getElementById("mainPic").onkeydown=null;
         var str="";
         for(i=0;i<iusedStars;i++)
         {
             str+="<div id=star"+(i+1)+" style='position:absolute; Top:"+ istarFrame_y[i]+"px; Left:"+istarFrame_x[i]+"px;"
                +"width:16px;height:16px;background-color:#FFEFFE; visibility:hidden; '>"
                +"<input id=info"+(i+1)+" type=hidden ></input>"
                +"<img id=img"+(i+1)+" /></div>";
         }
         alert(str);
         document.getElementById("stars").innerHTML=str;
    }

    /* 预载入图片*/
    function preload()
    {
         img1=new Image();
         img2=new Image();
         img1.src="http://my-first-test.googlecode.com/svn/trunk/vs2005/my/s1.gif";
         img2.src="http://my-first-test.googlecode.com/svn/trunk/vs2005/my/s2.gif";
    }

/*
    星星上天空飞行函数
*/
function starFly(x,y,k,step)
{   
    //var k=(endpoint.y-startpoint.y)*1.0/((startpoint.x-endpoint.x)*(startpoint.x-endpoint.x));
   var t=star.style.left;
   if(parseInt(t)>x)
     return;
   var s=parseInt(t)+step;
   star.style.left=s;
   s=Math.round(k*(s-x)*(s-x)-y);           //计算弧线y轴坐标
   star.style.top=-s;
   window.setTimeout(ymove,20);   
}

/* 用户许下自己的心愿，插入到一个新的星星中*/
function userStarValid()
{
    var text="";
    var s;
    s=document.getElementById("starname").value;
    s=trim(s);
    if(s=="")
    {
        alert("请输入星星名称");
        document.getElementById("starname").focus();
        return;
    }
    text+=s+"|";
    var radio=document.getElementsByName("starType");
    if(radio!=null)
    {
        for(var i=0; i<radio.length; i++)
            if(radio[i].checked)
            {
                s=trim(radio[i].value);
                text+=s+"|";
                break;
            }
    }
    s=document.getElementById("starspec").value;
    s=trim(s);
    if(s=="")
    {
        alert("请输入星星简要说明");
        document.getElementById("starspec").focus();
        return;
    }
    text+=s+"|";
    s=document.getElementById("starbless").value;
    s=trim(s);
    if(s=="")
    {
        alert("请输入星星祝福语");
        document.getElementById("starbless").focus();
        return;
    }
    text+=s;
    saveUser_star(text);
}

/* 用户修改自己的心愿*/
function userAlterStarValid()
{
    var text="";
    var s;
    s=document.getElementById("starname_a").value;
    s=trim(s);
    if(s=="")
    {
        alert("请输入星星名称");
        document.getElementById("starname_a").focus();
        return;
    }
    text+=s+"|";
    var radio=document.getElementsByName("starType_a");
    if(radio!=null)
    {
        for(var i=0; i<radio.length; i++)
            if(radio[i].checked)
            {
                s=trim(radio[i].value);
                text+=s+"|";
                break;
            }
    }
    s=document.getElementById("starspec_a").value;
    s=trim(s);
    if(s=="")
    {
        alert("请输入星星简要说明");
        document.getElementById("starspec_a").focus();
        return;
    }
    text+=s+"|";
    s=document.getElementById("starbless_a").value;
    s=trim(s);
    if(s=="")
    {
        alert("请输入星星祝福语");
        document.getElementById("starbless_a").focus();
        return;
    }
    text+=s;
    alterSelfBless(text);
}

//删除左右两端的空格
function trim(str)
{ 
    return str.replace(/(^\s*)|(\s*$)/g, ""); 
}

/*显示星星的详细说明div层*/

function $(element)
{
    return element = document.getElementById(element);
}

function userStarOnmouseover(id)
{
    iwaitTime++;
    if(iwaitTime>maxWait)
    {
        var star=$("star"+id);
        var info=$("info"+id).value;
        var obj=$("showInfo");
        obj.style.top=parseInt(star.style.top)+3+'px';
        obj.style.left=parseInt(star.style.left)+2+'px';
        
        var s=info.split('|');
        document.getElementById("starName_table").innerText=s[2];
        document.getElementById("starType_table").innerText=s[3];
        var friend;
        if(s[6]==owner.getId())
            friend=owner;
        else
            friend=viewerFriends.getById(s[6]);
        document.getElementById("starFounder_table").innerText=friend.getDisplayName();
        document.getElementById("starSepc_table").innerText=s[4];
        //document.getElementById("starSepc_table").innerText=obj.name;
        document.getElementById("starBless_table").innerText=s[5];
        
        obj.style.display='block';
    }
    else
        iInterval=window.setTimeout("userStarOnmouseover("+id+")",500); 
}

function userStarOnmouseout()
{
    window.clearTimeout(iInterval);
    if(iwaitTime>maxWait)
    {
        var obj=$("showInfo");
        obj.style.display='none';
    }
    iwaitTime=0;
}

/*地位某个用户的祝福星星*/
function locateUerBless(uid)
{
    var res=document.getElementsByTagName("input");
    var str="";
    var obj;
    if(res!=null)
    {
       // alert(res.length);
        for(var i=0; i<res.length; i++)
        {
            obj=res[i];
            var id=obj.id;
            if(id.substring(0,4)=="info")
            {
                id=id.substring(4,id.length);
                var s=obj.value.split('|');
                obj=document.getElementById("star"+id);
                //alert(id);
                if(obj!=null && s[6]==uid)
                {
                    var top=parseInt(obj.style.top);
                    var left=parseInt(obj.style.left);
                    var width=parseInt(obj.style.width);
                    var height=parseInt(obj.style.width);
                    str+="<DIV id=locateUser style=\"position:absolute; top:"+(top-30)+"; left:"+(left-30)
                       +"; BORDER-RIGHT: yellow 3px double; BORDER-TOP: yellow 3px double"
                       +" BACKGROUND: transprant; BORDER-LEFT: yellow 3px double; WIDTH:"+(width+60)
                       +"; BORDER-BOTTOM: yellow 3px double; HEIGHT:"+(height+60)+";\" align=left></DIV>";
                }
            }
         }
     }
        obj=document.getElementById("locateUserBless");
        if(obj!=null)
            obj.innerHTML=str;
        changeShapeLocate();
    }

        function changeShapeLocate()
        {
            var obj=document.getElementsByName("locateUser");
            for(var i=0; i<obj.length;i++)
            {
                var t=obj[i];
                var s=parseInt(t.style.width);
                if(s<=26)
                {
                    return;
                }
                t.style.width=s-2;
                t.style.height=s-2;
                t.style.top=parseInt(t.style.top)+1;
                t.style.left=parseInt(t.style.left)+1;
            }
            window.setTimeout(changeShapeLocate,80);
        }

        /*只显示当前浏览者的星星*/
        function showViewerBless()
        {
            var vid=viewer.getId();
            var res=document.getElementsByTagName("input");
            var str="";
            if(res!=null)
            {
                for(var i=0; i<res.length; i++)
                {
                    var obj=res[i];
                    var id=obj.id;
                    if(id.substring(0,4)=="info")
                    {
                        id=id.substring(4,id.length);
                        var s=obj.value.split('|');
                        if(obj!=null && s[6]==vid)
                        {
                             var j=parseInt(id);
                             str+="<div id=star"+j+" style='position:absolute; Top:"+ istarFrame_y[j-1]+"px; Left:"+istarFrame_x[j-1]+"px;"
                                +"width:16px;height:16px;background-color:#FFEFFE; visibility:visible;' onclick=showViewerInfo("+j+") >"
                                +"<input id=info"+j+" type=hidden value="+obj.value+"></input>"
                                +"<img id=img"+j+" src="+img2.src+" /></div>";
                        }
                    }
                }
              document.getElementById("stars").innerHTML=str;
            }
         }
         
         /*只显示当前浏览者的星星信息*/
         function showViewerInfo(id)
         {
            icurrStarId=id;
            var obj=document.getElementById("info"+id);
            if(obj!=null)
            {
                var s=obj.value.split('|');
                for(var i=0;i<s.length;i++)
                {
                    document.getElementById("starname_a").value=s[2];
                    var radio=document.getElementsByName("starType_a");
                    for(var j=0;j<radio.length;j++)
                    {
                         if(radio[j].value==s[3])
                        {
                            radio[j].checked='CHECKED';
                            break;
                        }
                    }
                    document.getElementById("starspec_a").value=s[4];
                    document.getElementById("starbless_a").value=s[5];
                }
            }
         }

    